import BuildForm from '@/components/BuildForm/BuildForm';
import FormattedMessageCustom from '@/components/FormattedMessageCustom/FormattedMessageCustom';
import { mapForm } from '@/pages/order/buildFormItems';
import { createAdvertBusiness } from '@/servers/api/userController';
import { formFormat } from '@/utils';
import { history, useIntl } from '@umijs/max';
import { Button, Space, Spin, message } from 'antd';
import { useEffect, useRef, useState } from 'react';
import { attract } from './attract';
import { useModel } from '@umijs/max';
import VipPopup from '@/components/VipPopup/VipPopup';

const AttractInvestment: React.FC = () => {
    const intl = useIntl();
    const formItems = useRef();
    const { initialState } = useModel('@@initialState');
    const { auth, isAuthName } = initialState as IGlobal.IInitialState;
    const { authModal } = useModel('useUser');
    const [form, setForm] = useState();
    const [loading, setloading] = useState(false);
    const [vipShow, setvipShow] = useState(false);
    function onFinish(val: IBuildForm.IDefault)  {
        if (auth && auth?.level > 0) {
            if (auth.auth.indexOf("ad") === -1) { // 非会员不能提交
                setvipShow(true);
                // 弹出vip购买弹窗
                return false
            } else {
                if (isAuthName?.status !== 2) {
                    authModal(
                        intl.formatMessage({ id: 'authText' }),
                        intl.formatMessage({ id: '立即认证' }),
                        intl.formatMessage({ id: '继续使用' }),
                    );
                    return;
                }
            }
        } else {
            setvipShow(true);
            // 弹出vip购买弹窗
            return false
        }
        const params = formFormat(val, formItems.current);
        setloading(true);
        createAdvertBusiness({
            ...params,
        })
            .then(() => {
                setloading(false);
                message.success(
                    intl.formatMessage({ id: '发布成功，请等待后台审核' }),
                );
                history.push('/home');
            })
            .catch((err) => {
                message.error(err.msg);
                setloading(false);
            });
    }
    useEffect(() => {
        let temp = JSON.parse(JSON.stringify(attract));
        formItems.current = temp;
        mapForm(formItems.current, 0).then((res) => {
            setForm(res);
        });
    }, []);
    return (
        <Spin spinning={loading}>
            <BuildForm
                formItems={formItems.current}
                value={form}
                onFinish={onFinish}
            >
                <Space>
                    <Button type="primary" htmlType="submit">
                        <FormattedMessageCustom id="发布"></FormattedMessageCustom>
                    </Button>
                </Space>
            </BuildForm>
            <VipPopup
                vipShow={vipShow}
                onChange={() => {
                    setvipShow(false);
                }}
            ></VipPopup>
        </Spin>
    );
};
export default AttractInvestment;
